package com.wh1200.mybatis.xmlless.model.base

import com.wh1200.kotlin.isNotNullAndNotBlank
import com.wh1200.mybatis.xmlless.annotations.Count
import com.wh1200.mybatis.xmlless.annotations.JoinTableColumn
import com.wh1200.mybatis.xmlless.enums.XmllessFieldFill
import com.wh1200.mybatis.xmlless.enums.XmllessFieldStrategy


/**
 *
 * @author 吴昊
 * @date 2024/1/19 10:40
 * @since v0.0.0
 * @version 1.0
 */
interface BaseTableFieldWrapper {

  /**
   * @return
   */
  fun isLogicDelete(): Boolean

  /**
   * @return
   */
  fun getLogicDelValueInner(): String?

  fun getLogicDelValue(): Any? {
    return parseLogicDelValue(isLogicDelete(), getLogicDelValueInner(), getFieldType())
  }

  /**
   * @return
   */
  fun getFieldType(): BaseSimpleClassWrapper

  /**
   * @return
   */
  fun getLogicNotDelValueInner(): String?

  fun getLogicNotDelValue(): Any? {
    return parseLogicNotDelValue(isLogicDelete(), getLogicNotDelValueInner(), getFieldType())
  }

  /**
   * @return
   */
  fun getFieldName(): String

  /**
   * @return
   */
  fun getColumn(): String

  /**
   * @return
   */
  fun getUpdateStrategy(): XmllessFieldStrategy

  /**
   * @return
   */
  fun getFieldFillStrategy(): XmllessFieldFill

  /**
   * @return
   */
  fun isCharSequence(): Boolean

  /**
   * @return
   */
  fun isMarkedCreatedBy(): Boolean

  fun isMarkedDeletedBy(): Boolean

  fun isMarkedDeletedDate(): Boolean

  /**
   * @return
   */
  fun isMarkedLastModifiedBy(): Boolean

  /**
   * @return
   */
  fun isMarkedCreatedDate(): Boolean

  /**
   * @return
   */
  fun isMarkedLastModifiedDate(): Boolean

  /**
   * @return
   */
  fun getCount(): Count?

  /**
   * 字段 update set 部分注入
   * @return
   */
  fun getUpdate(): String?

  /**
   * @return
   */
  fun getJoinTableColumn(): JoinTableColumn?

  /**
   * @return
   */
  fun getSelectedProperties(): List<String>


  /**
   * 解析逻辑删除状态值
   *
   * @param isLogicDelete
   * @param logicDeleteValue
   * @param propertyType
   * @return
   */
  private fun parseLogicDelValue(
      isLogicDelete: Boolean,
      logicDeleteValue: String?,
      propertyType: BaseSimpleClassWrapper
  ): Any? {
    return parseLogicFlagValue(
        if (isLogicDelete) {
          if (logicDeleteValue.isNotNullAndNotBlank()) {
            logicDeleteValue
          } else if (propertyType.isBoolean()) {
            "true"
          } else {
            "1"
          }
        } else {
          null
        }, propertyType
    )
  }

  /**
   * 解析非逻辑删除状态值
   * @param logicDelete
   * @param logicNotDeleteValue
   * @param propertyType
   * @return
   */
  private fun parseLogicNotDelValue(
      logicDelete: Boolean,
      logicNotDeleteValue: String?,
      propertyType: BaseSimpleClassWrapper
  ): Any? {
    return parseLogicFlagValue(
        if (logicDelete) {
          if (logicNotDeleteValue.isNotNullAndNotBlank()) {
            logicNotDeleteValue
          } else if (propertyType.isBoolean()) {
            "false"
          } else {
            "0"
          }
        } else {
          null
        },
        propertyType
    )
  }

  /**
   * 将逻辑删除的状态值转换为对应的类型
   *
   * @param value
   * @param type
   * @return
   */
  private fun parseLogicFlagValue(value: String?, type: BaseSimpleClassWrapper): Any? {
    if (value == null) {
      return null
    }
    if (type.isString()) {
      return value
    } else if (type.isBoolean()) {
      return value.toBoolean()
    }
    return value.toInt()
  }

  fun getInsertStrategy(): XmllessFieldStrategy

}
